#include "mainwindow.h"
#include "graphicsview.h"
#include "3rdparty/SARibbon/saribbon.h"
#include "qactiongroup.h"
#include <QColorDialog>
#include <QStatusBar>
#include <QFileDialog>
#include <QMessageBox>
#include <QPixmap>
#include <QGraphicsPixmapItem>




MainWindow::MainWindow(QWidget *parent)
    : SARibbonMainWindow(parent)
{
    //检测icon完整
    auto checkIcon = [](const QString& path) {
        if(!QFile::exists(path)) {
            qWarning() << "资源缺失:" << path;
            return false;
        }
        QPixmap pm(path);
        if(pm.isNull()) {
            qWarning() << "资源无效:" << path;
            return false;
        }
        qDebug() << "确认资源可用：" << path << "size:" << pm.size();
        return true;
    };
    checkIcon(":/icon/icons/open.png");
    checkIcon(":/icon/icons/saveas.png");
    checkIcon(":/icon/icons/select.png");
    checkIcon(":/icon/icons/point.png");
    checkIcon(":/icon/icons/line.png");
    checkIcon(":/icon/icons/circle.png");
    checkIcon(":/icon/icons/rectangle.png");
    checkIcon(":/icon/icons/delete.png");
    checkIcon(":/icon/icons/color.png");
    checkIcon(":/icon/icons/clear.png");
    checkIcon(":/icon/icons/select.png");
    checkIcon(":/icon/icons/logo.png");


    this->setWindowTitle("图形绘制系统 - Qt 6.5.3");
    this -> setWindowIcon(QIcon(":/icon/icons/logo.png"));

    SARibbonBar* ribbonBar = this->ribbonBar();

    //ribbonBar->applicationButton()->setText("文件");


    // 创建文件菜单
    SARibbonCategory* fileCategory = ribbonBar->addCategoryPage("文件");
    SARibbonPannel* filePannel = fileCategory->addPannel("外部文件");
    QAction* openAction = new QAction(
        QIcon(":/icon/icons/open.png"),
        "打开", this);
    filePannel ->addAction(openAction);
    QAction* saveAsAction = new QAction(
        QIcon(":/icon/icons/saveas.png"),
        "另存为", this);
    filePannel ->addAction(saveAsAction);
    // =========================================

    // 创建绘图分类
    SARibbonCategory* drawCategory = ribbonBar->addCategoryPage("绘图");

    //创建基本图形面板
    SARibbonPannel* basicPannel = drawCategory->addPannel("基本图形");

    // 创建图形动作组
    QActionGroup* drawActionGroup = new QActionGroup(this);
    drawActionGroup->setExclusive(true);

    QAction* selectAction = new QAction(
        QIcon(QPixmap(":/icon/icons/select.png")),
        "选择", this);
    selectAction->setCheckable(true);
    selectAction->setChecked(false);
    drawActionGroup->addAction(selectAction);
    basicPannel->addAction(selectAction);

    QAction* pointAction = new QAction(
        QIcon(":/icon/icons/point.png"),
        "点", this);
    pointAction->setCheckable(true);
    pointAction->setChecked(false);
    drawActionGroup->addAction(pointAction);
    basicPannel->addAction(pointAction);

    QAction* lineAction = new QAction(
        QIcon(":/icon/icons/line.png"),
        "直线", this);
    lineAction->setCheckable(true);
    lineAction->setChecked(false);
    drawActionGroup->addAction(lineAction);
    basicPannel->addAction(lineAction);

    QAction* rectAction = new QAction(
        QIcon(":/icon/icons/rectangle.png"),
        "长方形", this);
    rectAction->setCheckable(true);
    rectAction->setChecked(false);
    drawActionGroup->addAction(rectAction);
    basicPannel->addAction(rectAction);

    QAction* circleAction = new QAction(
        QIcon(":/icon/icons/circle.png"),
        "圆形", this);
    circleAction->setCheckable(true);
    circleAction->setChecked(false);
    drawActionGroup->addAction(circleAction);
    basicPannel->addAction(circleAction);

    //  创建编辑面板
    SARibbonPannel* editPannel = drawCategory->addPannel("编辑");
    QAction* deleteAction = new QAction(
        QIcon(":/icon/icons/delete.png"),
        "删除", this);
    editPannel->addAction(deleteAction);

    QAction* colorAction = new QAction(
        QIcon(":/icon/icons/color.png"),
        "颜色", this);
    editPannel->addAction(colorAction);

    QAction* clearAction = new QAction(
        QIcon(":/icon/icons/clear.png"),
        "清空", this);
    editPannel->addAction(clearAction);

    // 中心窗口
    graphicsView = new GraphicsView(this);
    setCentralWidget(graphicsView);
    currentColor = Qt::black;

    //连接信号槽
    connect(openAction, &QAction::triggered, this, [this](){
        QString filePath = QFileDialog::getOpenFileName(
            this,
            "打开图像文件",
            "",
            "PNG图像 (*.png);;所有文件 (*.*)");

        if (!filePath.isEmpty()) {
            QPixmap pixmap(filePath);
            if (!pixmap.isNull()) {
                graphicsView->clearAllShapes();
                QGraphicsScene* scene = graphicsView->scene();
                scene->addPixmap(pixmap)->setPos(0, 0);
                statusBar()->showMessage("已加载图像: " + filePath, 3000);
            } else {
                QMessageBox::warning(this, "错误", "无法加载图像文件");
            }
        }
    });

    connect(saveAsAction, &QAction::triggered, this, [this](){
        QString filePath = QFileDialog::getSaveFileName(
            this,
            "保存为PNG图像",
            "",
            "PNG图像 (*.png)");

        if (!filePath.isEmpty()) {
            // 确保扩展名正确
            if (!filePath.endsWith(".png", Qt::CaseInsensitive)) {
                filePath += ".png";
            }

            QPixmap pixmap(graphicsView->scene()->sceneRect().size().toSize());
            pixmap.fill(Qt::white);

            QPainter painter(&pixmap);
            graphicsView->scene()->render(&painter);

            if (pixmap.save(filePath, "PNG")) {
                statusBar()->showMessage("图像已保存: " + filePath, 3000);
            } else {
                QMessageBox::warning(this, "错误", "保存图像失败");
            }
        }
    });
    connect(selectAction, &QAction::triggered, this, [this](){
        graphicsView->setCurrentShapeType(ShapeType::None);
        statusBar()->showMessage("当前模式：选择/移动");
    });

    connect(pointAction, &QAction::triggered, this, [this](){
        graphicsView->setCurrentShapeType(ShapeType::Point);
        statusBar()->showMessage("当前模式：绘制点");
    });

    connect(lineAction, &QAction::triggered, this, [this](){
        graphicsView->setCurrentShapeType(ShapeType::Line);
        statusBar()->showMessage("当前模式：绘制直线");
    });

    connect(rectAction, &QAction::triggered, this, [this](){
        graphicsView->setCurrentShapeType(ShapeType::Rectangle);
        statusBar()->showMessage("当前模式：绘制长方形");
    });

    connect(circleAction, &QAction::triggered, this, [this](){
        graphicsView->setCurrentShapeType(ShapeType::Circle);
        statusBar()->showMessage("当前模式：绘制圆形");
    });

    connect(deleteAction, &QAction::triggered, this, [this](){
        graphicsView->deleteSelectedShape();
    });

    connect(colorAction, &QAction::triggered, this, [this](){
        QColor color = QColorDialog::getColor(currentColor, this, "选择颜色");
        if (color.isValid()) {
            currentColor = color;
            graphicsView->setCurrentColor(color);
        }
    });

    connect(clearAction, &QAction::triggered, this, [this](){
        graphicsView->clearAllShapes();
    });


    // 7. 设置初始状态
    resize(1000, 700);
    statusBar()->showMessage("就绪", 2000);
}

MainWindow::~MainWindow()
{
    // 无需手动删除成员
}
